package cn.nuist.controller;

import cn.nuist.entity.Customer;
import cn.nuist.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Controller
@RequestMapping("customer")
public class CustomerController {

    private CustomerService customerService;

    @Autowired
    public CustomerController(CustomerService customerService) {
        this.customerService = customerService;
    }

    @RequestMapping("getCustomerList")
    public String customerList(Model model){
        List<Customer> allCustomer = customerService.getAllCustomer();
        model.addAttribute("customers",allCustomer);
        return "customer/customerList";
    }

    @RequestMapping("addCustomer")
    public String add(Customer customer){
        customer.setRegisterDate(new Date());
        System.out.println(customer);
        customerService.addCustomer(customer);
        return "redirect:/customer/getCustomerList";
    }

    @RequestMapping("delete")
    public String deleteCustomer(@RequestParam("customerID") int customerID){
        customerService.delete(customerID);
        return "redirect:/customer/getCustomerList";
    }

    @RequestMapping("showdetail")
    public String showdetail(@RequestParam("customerID") int customerID,Model model){
        //System.out.println(customerID);
        Customer customerById = customerService.getCustomerById(customerID);
        model.addAttribute("customer",customerById);
        Instant instant = customerById.getRegisterDate().toInstant();
        ZonedDateTime zdt = ZonedDateTime.ofInstant(instant, ZoneId.systemDefault());
        // 定义日期格式，同样以"年-月-日 时:分:秒"格式为例
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String dateString = zdt.format(dtf);
        //System.out.println(dateString);
        model.addAttribute("date",dateString);
        return "customer/customerView";
    }

    @RequestMapping("updateView")
    public String updateView(@RequestParam("customerID") int customerID,Model model){
        Customer customerById = customerService.getCustomerById(customerID);
        model.addAttribute("customer",customerById);
        Instant instant = customerById.getRegisterDate().toInstant();
        ZonedDateTime zdt = ZonedDateTime.ofInstant(instant, ZoneId.systemDefault());
        // 定义日期格式，同样以"年-月-日 时:分:秒"格式为例
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String dateString = zdt.format(dtf);
        //System.out.println(dateString);
        model.addAttribute("date",dateString);
        return "customer/customerUpdate";
    }

    @RequestMapping("update")
    public String update(Customer customer,Model model){
        /*System.out.println("****"+customer);
        System.out.println("****"+customer.getRegisterDate());*/
        customerService.update(customer);
        return "redirect:/customer/showdetail?customerID=" + customer.getCustomerID();
    }

    @RequestMapping("search")
    public String search(String customerName,Model model){
        Customer customerByName = customerService.getCustomerByName(customerName);
        if(Objects.isNull(customerByName)){
            return "redirect:/customer/getCustomerList";
        }else{
            int id = customerByName.getCustomerID();
            return "redirect:/customer/showdetail?customerID="+id;
        }

    }

}
